package com.blinddate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blinddate.context.BaseContext;
import com.blinddate.mapper.InterestMapper;
import com.blinddate.mapper.UserInfMapper;
import com.blinddate.pojo.entity.Interest;
import com.blinddate.pojo.entity.UserInf;
import com.blinddate.pojo.vo.UserInfVO;
import com.blinddate.service.InterestService;
import com.blinddate.service.UserInfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class
InterestServiceImpl implements InterestService {
    @Autowired
    private InterestMapper interestMapper;
    @Autowired
    private UserInfMapper userInfMapper;

    @Autowired
    private UserInfService userInfService;

    /**
     * 展示用户列表
     */
    public List<UserInfVO> list(Integer page, Integer size) {//收藏列表
        IPage iPage=new Page(page,size);
        //条件判断
        LambdaQueryWrapper<Interest> lqw =new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        lqw.eq(Interest::getUserId,userId);
        interestMapper.selectPage(iPage,lqw);
        //获得列表
        List<Interest> interests=iPage.getRecords();
        List<UserInfVO> userInfList=new ArrayList<>();
        for (Interest interest : interests) {//获取用户信息  并添加相关字段
            LambdaQueryWrapper<UserInf> lq =new LambdaQueryWrapper<UserInf>();
            lq.eq(UserInf::getUserId,interest.getGetId());
            UserInf userInf=userInfMapper.selectOne(lq);
            if (userInf!=null){
                UserInfVO userInfVO=new UserInfVO();
                userInfVO.setUserInf(userInf);
                userInfService.showAllInf(userInfVO,userId,interest.getGetId());
                userInfList.add(userInfVO);
            }
        }
        return userInfList;
    }

    /**
     * 收藏用户
     * @param id
     */
    public Boolean add(Long id) {//添加关注
        LambdaUpdateWrapper<UserInf> userInfLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userInfLambdaUpdateWrapper.eq(UserInf::getUserId,id);
        UserInf userInf = userInfService.getOne(userInfLambdaUpdateWrapper);
        if (userInf==null){
            return false;
        }
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Interest> interestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        interestLambdaQueryWrapper.eq(Interest::getUserId,userId).eq(Interest::getGetId,id);
        Interest one = interestMapper.selectOne(interestLambdaQueryWrapper);
        if (one!=null){//判断是否有重复判断
            return false;
        }
        Interest interest=new Interest();
        interest.setUserId(userId);
        interest.setGetId(id);
        interestMapper.insert(interest);
        return true;
    }

    /**
     * 取消收藏用户
     * @param id
     */
    public void delete(Long id) {
        LambdaQueryWrapper<Interest> lqw=new LambdaQueryWrapper<Interest>();
        lqw.eq(Interest::getGetId,id).eq(Interest::getUserId,BaseContext.getCurrentId());
        interestMapper.delete(lqw);
    }

    @Override
    public List<UserInfVO> whoInterestList(Integer page, Integer size) {
        Long userId = BaseContext.getCurrentId();
        IPage iPage = new Page(page,size);
        //被关注表的条件判断
        LambdaQueryWrapper<Interest> interestLambdaQueryWrapper = new LambdaQueryWrapper<>();
        interestLambdaQueryWrapper.eq(Interest::getGetId,userId);
        interestMapper.selectPage(iPage,interestLambdaQueryWrapper);
        //获取列表
        List<Interest> list = iPage.getRecords();
        List<UserInfVO> userInfVOList = new ArrayList<>();
        for (Interest interest : list) {//获用户列表并提交相关字段
            log.info("参数interest:{}",interest);
            LambdaQueryWrapper<UserInf> userInfLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userInfLambdaQueryWrapper.eq(UserInf::getUserId,interest.getUserId());
            UserInf userInf = userInfMapper.selectOne(userInfLambdaQueryWrapper);
            if (userInf!=null){
                UserInfVO userInfVO = new UserInfVO();
                userInfVO.setUserInf(userInf);
                UserInfVO userInfVo = userInfService.showAllInf(userInfVO, userId, interest.getUserId());
                userInfVOList.add(userInfVo);
            }
        }
        return userInfVOList;
    }

    @Override
    public Long interestCont() {
        Long userId = BaseContext.getCurrentId();
        LambdaUpdateWrapper<Interest> interestLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        interestLambdaUpdateWrapper.eq(Interest::getUserId,userId);
        Long count = interestMapper.selectCount(interestLambdaUpdateWrapper);
        return count;

    }
}
